package com.yanqu.road.server.manger.activity.jianchuanghua;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.args.JianChuangHuaCombArgs;
import com.yanqu.road.entity.activity.jianchuanghua.ChuangHuaComb;
import com.yanqu.road.entity.activity.jianchuanghua.ChuangHuaInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.bussiness.activity.JianChuangHuaBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.jianchuanghua.JianChuangHuaModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

public class JianChuangHuaMgr extends TempMgr {

    private static ActivityInfo activityInfo;
    private static List<ChuangHuaComb> chuangHuaCombList;
    private static List<ChuangHuaInfo> chuangHuaInfoList;
    public static int scoreItemId = eItemId.JIAN_CHUANG_HUA_SCORE.getValue();
    public static int scissorsItemId = eItemId.JIAN_CHUANG_HUA_SCISSORS.getValue();

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityData() {
        getLogger().info("reload JianChuangHua start");

        // 获取进行中的活动
        ActivityInfo inShowTimeActivityInfo = null;
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.JianChuangHuaActivity.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no WishPool in show time");
            return;
        }
        inShowTimeActivityInfo = openActivityInfoList.get(0);

        // 道具ID
        List<Integer> param4List = StringUtils.stringToIntegerList(inShowTimeActivityInfo.getParam4(), "\\|");
        if (param4List.size() <= 0 || param4List.get(0).equals(0) || param4List.get(1).equals(0)) {
            getLogger().error("JianChuangHuaMgr error on param4, itemId error!");
        } else {
            scissorsItemId = param4List.get(0);
            scoreItemId = param4List.get(1);
        }

        // 读配置
        List<ChuangHuaInfo> tmpChuangHuaInfoList = JianChuangHuaBussiness.getChuangHuaInfoList(inShowTimeActivityInfo.getActivityId());
        List<ChuangHuaComb> tmpChuangHuaCombList = JianChuangHuaBussiness.getChuangHuaCombList(inShowTimeActivityInfo.getActivityId());

        // 赋值
        chuangHuaInfoList = tmpChuangHuaInfoList;
        chuangHuaCombList = tmpChuangHuaCombList;
        activityInfo = inShowTimeActivityInfo;

        reloadPlayerData();

        getLogger().info("reload JianChuangHua end");
    }

    /**
     * 获取展示中的活动ID（0表示没活动）
     */
    public static int getShowTimeActivityId() {
        return activityInfo == null ? 0 : activityInfo.getActivityId();
    }

    /**
     * 是否在展示时间内
     */
    public static boolean isActivityInShowTime() {
        return ActivityMgr.activityInShowTime(activityInfo);
    }

    /**
     * 是否在操作时间内
     */
    public static boolean isActivityInTime() {
        return ActivityMgr.activityInTime(activityInfo);
    }

    /**
     * 是否在清空道具时间内
     */
    public static boolean isActivityInClearTime() {
        return ActivityMgr.activityInShowPeriod(activityInfo) || ActivityMgr.activityInRewardPeriod(activityInfo);
    }

    /**
     * 获取随机窗花
     */
    public static List<ChuangHuaInfo> getRandomChuangHuaInfoList(int times, Random random) {
        List<ChuangHuaInfo> result = new ArrayList<>();
        List<Integer> weightList = chuangHuaInfoList.stream().map(ChuangHuaInfo::getWeight).collect(Collectors.toList());
        for (int i = 0; i < times; i++) {
            int index = RandomHelper.getRandomIndexByWeight(weightList, random);
            result.add(chuangHuaInfoList.get(index));
        }
        return result;
    }

    /**
     * 更新窗花组合次数
     */
    public static void updateChuangHuaCombTimes(GamePlayer player, Property chuangHuaProp) {
        for (ChuangHuaComb chuangHuaComb : chuangHuaCombList) {
            int times = Integer.MAX_VALUE;
            Property elementProp = chuangHuaComb.getElementProp();
            Map<Integer, BigInteger> elementGoods = elementProp.getGoods();
            for (Map.Entry<Integer, BigInteger> entry : elementGoods.entrySet()) {
                int haveCount = chuangHuaProp.getCountByGoodsId(entry.getKey()).intValue();
                int needCount = entry.getValue().intValue();
                if (haveCount >= needCount) {
                    int curTimes = haveCount / needCount;
                    times = times < curTimes ? times : curTimes;
                } else {
                    times = 0;
                    break;
                }
            }
            player.notifyListener(eGamePlayerEventType.JianChuangHuaCombTimes.getValue(), new JianChuangHuaCombArgs(chuangHuaComb.getCombId(), times));
        }
    }

    /**
     * 在线玩家加载活动数据
     */
    public static void reloadPlayerData() {
        if (isActivityInShowTime()) {
            int activityId = getShowTimeActivityId();
            for (GamePlayer player : GamePlayerMgr.getCopyPlayerMap().values()) {
                if (SystemOpenMgr.systemOpen(player, eSystemId.JianChuangHuaActivity.getValue())) {
                    JianChuangHuaModule jchModule = player.getModule(JianChuangHuaModule.class);
                    if (jchModule.getUserData() == null || jchModule.getUserData().getActivityId() != activityId) {
                        jchModule.initUserData();
                        jchModule.syncJianChuangHuaConfig();
                        jchModule.syncChuangHuaCount();
                    }
                }
            }
        }
    }

    public static List<ChuangHuaComb> getChuangHuaCombList() {
        return chuangHuaCombList;
    }

    public static List<ChuangHuaInfo> getChuangHuaInfoList() {
        return chuangHuaInfoList;
    }
}
